/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package operator.server;

import java.io.IOException;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.logging.Level;
import java.util.logging.Logger;
import share.Listener;

/**
 *
 * @author mire
 */
public class OperatorClientListener extends Listener implements Runnable {
    
    private OperatorServer operatorServer;
    private Thread runningThread = null;
    
    public OperatorClientListener(OperatorServer operatorServer, ExecutorService threadPool) {
        super(operatorServer.getClientPort(), threadPool);
        this.operatorServer = operatorServer;
    }
    
    @Override
    public void run() {
        synchronized (this) {
            runningThread = Thread.currentThread();
        }
        
        openServerSocket();
        while (!isStoped()) {
            Socket clientSocket = null;
            try {
                clientSocket = serverSocket.accept();
                
//                String address = clientSocket.getInetAddress().getHostAddress();
//                if (!operatorServer.getClients().containsKey(address)) {
//                    System.out.println("ERROR_UNKOWN_OPERATOR " + address);
//                    clientSocket.close();
//                    continue;
//                }
            } catch (IOException ex) {
                Logger.getLogger(OperatorCentralListener.class.getName()).log(Level.SEVERE, null, ex);
            }
            
            threadPool.execute(new OperatorClientThread(clientSocket, operatorServer));
        }
    }
}
